package com.zte.market.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zte.market.model.Custom;
import com.zte.market.model.CustomCondition;
import com.zte.market.service.CustomService;
import com.zte.util.OperatorLog;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/custom")
public class CustomAction {
    @Autowired
    private CustomService customService;

    //获取分页信息
    @PostMapping("/page")
    @OperatorLog(operate="多条件查询客户分页", module="客户管理")
    public ResultMessage pageCustomByCondition(@RequestBody CustomCondition condition) {
        int pageSize = condition.getPageSize();
        int pageNum = condition.getPageNum();
        if (pageSize < 0) pageSize = 5;
        if (pageNum < 0) pageNum = 1;
        PageModel<Custom> pageModel = new PageModel<>();
        pageModel.setPageNum(pageNum);
        pageModel.setPageSize(pageSize);
        return new ResultMessage(200, true, "scuess", customService.pageCustomInfoByCondition(pageModel, condition));
    }

    //更改角色状态
    @PutMapping("/{customId}/{status}")
    @OperatorLog(operate="更改角色状态", module="客户管理")
    public ResultMessage changeCustomStatus(@RequestBody CustomCondition condition, @PathVariable String customId, @PathVariable int status) {
        if (status == 0) {
             customService.update(new LambdaUpdateWrapper<Custom>().eq(Custom::getCustomid, customId).set(Custom::getStatus, 1));
        } else {
            customService.update(new LambdaUpdateWrapper<Custom>().eq(Custom::getCustomid, customId).set(Custom::getStatus, 0));
        }
        return pageCustomByCondition(condition);
    }

    //添加客户
    @OperatorLog(operate="添加客户", module="客户管理")
    @PostMapping
    public ResultMessage AddCustomStatus(@RequestBody Custom custom) {
        //custom.setCustomid(new Date().getTime());
        boolean saveSuccess = customService.save(custom);
        if (saveSuccess) {
            return new ResultMessage(200, true, "success", null);
        } else return new ResultMessage(400, false, "faild", null);
    }

    //分配客户
    @OperatorLog(operate="分配客户", module="客户管理")
    @PutMapping("/distributeCustom/{customId}/{userId}")
    public ResultMessage distributeCustom(@RequestBody CustomCondition condition,@PathVariable Integer customId, @PathVariable Integer userId) {
       boolean isSuccess= customService.update(new LambdaUpdateWrapper<Custom>().eq(Custom::getCustomid, customId).set(Custom::getDistractime, new Date()).set(Custom::getServiceid,userId));
       if(isSuccess){
           return pageCustomByCondition(condition);
       }else return new ResultMessage(400,false,"faild",null);

    }
    //修改客户信息
    @OperatorLog(operate="修改客户信息", module="客户管理")
    @PutMapping
    public ResultMessage updateCustomById(@RequestBody Custom custom){
        boolean isScuess=customService.update(new LambdaUpdateWrapper<Custom>().eq(Custom::getCustomid,custom.getCustomid())
                .set(Custom::getCustomname,custom.getCustomname())
                .set(Custom::getSex,custom.getSex())
                .set(Custom::getCompany,custom.getCompany())
                .set(Custom::getTelephone,custom.getTelephone())
                .set(Custom::getHomeaddress,custom.getHomeaddress())
                .set(Custom::getAddress,custom.getAddress())
                .set(Custom::getDistract,custom.getDistract())
                .set(Custom::getStatus,custom.getStatus())
                .set(Custom::getCreateid,custom.getCreateid())
                .set(Custom::getCreatetime,custom.getCreatetime()));
       if(isScuess){
           return new ResultMessage(200,true,"success",null);
       }else  return new ResultMessage(400,false,"fail",null);
    }
    //根据服务人员id查询客户
    @OperatorLog(operate="服务人员客户信息", module="客户管理")
    @PostMapping("/pageByUid")
    public ResultMessage pageCustomByConditionAndUid(@RequestBody CustomCondition condition){
        int pageSize = condition.getPageSize();
        int pageNum = condition.getPageNum();
        if (pageSize <=0) pageSize = 5;
        if (pageNum <= 0) pageNum = 1;
        PageModel<Custom> pageModel = new PageModel<>();
        pageModel.setPageNum(pageNum);
        pageModel.setPageSize(pageSize);
        return new ResultMessage(200, true, "scuess", customService.pageCustomInfoByConditionAndUid(pageModel, condition));
    }
    @OperatorLog(operate="查看客户详情", module="客户管理")
    @GetMapping("/{customId}")
    public ResultMessage getCustomInfoById(@PathVariable Integer customId){
        return new ResultMessage(200,true,"获取成功",customService.getCustomById(customId));
    }
    //查看用户名是否存在
    @OperatorLog(operate="验证客户姓名", module="客户管理")
    @GetMapping("/isExist/{customName}")
    public ResultMessage checkCustomName(@PathVariable String customName){
        Custom custom=customService.getOne(new LambdaQueryWrapper<Custom>().select(Custom::getCustomid)
                .eq(Custom::getCustomname,customName)
                .eq(Custom::getStatus,1));
        if(custom!=null){
            return new ResultMessage(200,true,"用户名存在",null);
        }
        return new ResultMessage(200,false,"用户名不存在",null);
    }
}
